package com.kaguya.vams.service.impl;

import cn.hutool.core.lang.Dict;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaguya.vams.domain.entity.SysDict;
import com.kaguya.vams.domain.vo.DictVo;
import com.kaguya.vams.mapper.SysDictMapper;
import com.kaguya.vams.service.SysDictService;
import com.kaguya.vams.service.listener.DictExcelListener;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数字字典 实现类
 * @author : kaguya
 * @date : 10:26 2021/8/10
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {
    @Override
    public List<SysDict> findChildData(Long id) {
        QueryWrapper<SysDict> qw = new QueryWrapper<SysDict>().eq("parent_id", id);
        List<SysDict> list = baseMapper.selectList(qw);
        //循环设置是否有子节点
        list = list.stream().map( e -> {
            e.setHasChildren(this.hasChild(e.getId()));
            return  e;
        }).collect(Collectors.toList());

        return list;
    }

    /**
     * 导入
     * @param file
     */
    @Override
    public void importFile(MultipartFile file) {

        try {
            EasyExcel
                    .read(file.getInputStream(), DictVo.class,new DictExcelListener(this))
                    .sheet()
                    .doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出
     * @param response
     */
    @Override
    public void exportFile(HttpServletResponse response) {
        //1.设置下载信息
        response.setContentType("application/vnd. ms-excel");
        //2.设置字符集
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //3.设置请求头信息以及文件名称
        String fileName = "dict";
        response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");

        //数据库查询并通过easyExcel执行写操作
        List<SysDict> dictList = baseMapper.selectList(null);

        //转化成vo 使用stream流进行对集合的操作
        List<DictVo> DictVoList = dictList.stream().map(item ->{
            //转换赋值
            DictVo DictVo = new DictVo();

            //使用Spring的BeanUtils来进行vo和实体类的转换
            BeanUtils.copyProperties(item,DictVo);

            return DictVo;
        }).collect(Collectors.toList());

        //使用easyExcel进行写入操作
        try {
            //write(输出流,vo类名).sheet(工作蒲名).数据
            EasyExcel.write(response.getOutputStream(),DictVo.class)
                    .sheet("dict")
                    .doWrite(DictVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断是否有子节点
     * @return
     */
    public boolean hasChild(Long id){
        QueryWrapper<SysDict> qw = new QueryWrapper<SysDict>().eq("parent_id", id);

        int count = this.count(qw);
        return count > 0;
    }


}
